//
//  AccountLanguageVC.m
//  ImGuider
//
//  Created by llt on 2017/8/21.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "AccountLanguageVC.h"
#import "AccountCell.h"
#import "MainTabBarVC.h"
#import "PlayerItem.h"
//#import "MainViewController.h"

@interface AccountLanguageVC ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *commitBtn;

@property (nonatomic, strong) NSArray *languageArray;

@property (nonatomic) AppLanguageType currentLanguage;
@end

@implementation AccountLanguageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup {
    
    self.title = MYLocalizedString(@"SETTING_LANGUAGE_SET", nil);
    
    self.languageArray = @[
                           @{@"type" : @(AppLanguageCNJan), @"name" : @"简体中文"},
                           @{@"type" : @(AppLanguageCNFan), @"name" : @"繁體中文（台灣）"},
                           @{@"type" : @(AppLanguageCNFanXG), @"name" : @"繁體中文（香港）"},
                           @{@"type" : @(AppLanguageEnglish), @"name" : @"English"},
                           @{@"type" : @(AppLanguageJapanese), @"name" : @"日本語"},
                           @{@"type" : @(AppLanguageItaliano), @"name" : @"Italiano"}
                           ];
    
    self.currentLanguage = [AppManager sharedManager].language;
    
    [self.commitBtn setTitle:MYLocalizedString(@"PUBLIC_CONFIRM", nil) forState:UIControlStateNormal];
    self.tableView.tableFooterView = [UIView new];
    
    [self.tableView reloadData];
}
- (IBAction)settingLan:(id)sender {
    
    if ([AppManager sharedManager].language == self.currentLanguage) {
        
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    AppManager.sharedManager.language = self.currentLanguage;
    
    
    MainTabBarVC *tabVC = [MainTabBarVC new];
    
    [UIApplication sharedApplication].keyWindow.rootViewController = tabVC;
    
    
    [[PlayerItem playItemShareManager] show];
    
    [NetAPIManager getAppVersionBlock:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.languageArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSDictionary *languageDict = self.languageArray[indexPath.row];
    
    AccountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountLanguageCell"];
    
    [cell configWithDict:languageDict];
    
    
    BOOL choosed = [languageDict[@"type"] integerValue] == self.currentLanguage;
    
    cell.choosed = choosed;
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    NSDictionary *languageDict = self.languageArray[indexPath.row];
    
    self.currentLanguage =  [languageDict[@"type"] integerValue];
    
    [tableView reloadData];
}




@end
